#!/usr/bin/python

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from critical_inner_polygon import critical_inner_polygon
from critical_inner_polygon import parse_input_file
import os

for filename in os.listdir('.'):
    if filename.endswith('.poly.txt'):
        p,q = parse_input_file(filename)
        free = critical_inner_polygon(p, q)
        
        buffer_poly = Polygon([(x*1.1, y*1.1) for x,y in p], facecolor='none', edgecolor='none')
        
        poly1 = Polygon(p,facecolor='none',edgecolor='green')
        poly2 = Polygon(q,facecolor='blue',edgecolor='none')


        fig, ax = plt.subplots()
        plt.gca().add_patch(buffer_poly)
        plt.gca().add_patch(poly2)
        if free:
            poly3 = Polygon(free, facecolor = 'none', edgecolor='red')
            plt.gca().add_patch(poly3)
            plt.title(filename + " : Solution Found")
        else:
            plt.title(filename + " : No solution")
        plt.gca().add_patch(poly1)
        ax.autoscale_view(tight=False)
        plt.savefig(filename[:-3] + 'png')
        plt.clf()
